/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.conversion_service;

import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class ConversionResponseParser {

	private Document _XMLDocument;
	
	
	/*
	 * Constructor
	 */
	public ConversionResponseParser(String XMLData) {
		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Parse conversion list
	 */
	public ArrayList<HashMap<String, String>> getConversionList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {
	        
			//Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
			
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList name = element.getElementsByTagName("name");
	        	temp.put("name", XMLUtils.getCharacterDataFromElement((Element) name.item(0)));

	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));
	        	
	        	NodeList category = element.getElementsByTagName("category");
	        	temp.put("category", XMLUtils.getCharacterDataFromElement((Element) category.item(0)));

	        	NodeList conversionValue = element.getElementsByTagName("conversionValue");
	        	Element conversionValueElement = (Element) conversionValue.item(0);
	        	NodeList conversionValueAmount = conversionValueElement.getElementsByTagName("microAmount");
	        	temp.put("conversionValue", 
	        			Converter.microamountToStr(XMLUtils.getCharacterDataFromElement((Element) conversionValueAmount.item(0))
	        					));

	        	NodeList numConversionEvents = element.getElementsByTagName("numConversionEvents");
	        	temp.put("numConversionEvents", XMLUtils.getCharacterDataFromElement((Element) numConversionEvents.item(0)));
	        	        		        	
	        	NodeList markupLanguage = element.getElementsByTagName("markupLanguage");
	        	temp.put("markupLanguage", XMLUtils.getCharacterDataFromElement((Element) markupLanguage.item(0)));

	        	NodeList httpProtocol = element.getElementsByTagName("httpProtocol");
	        	temp.put("httpProtocol", XMLUtils.getCharacterDataFromElement((Element) httpProtocol.item(0)));

	        	NodeList textFormat = element.getElementsByTagName("textFormat");
	        	temp.put("textFormat", XMLUtils.getCharacterDataFromElement((Element) textFormat.item(0)));

	        	NodeList conversionPageLanguage = element.getElementsByTagName("conversionPageLanguage");
	        	temp.put("conversionPageLanguage", XMLUtils.getCharacterDataFromElement((Element) conversionPageLanguage.item(0)));
	        	
	        	NodeList backgroundColor = element.getElementsByTagName("backgroundColor");
	        	temp.put("backgroundColor", XMLUtils.getCharacterDataFromElement((Element) backgroundColor.item(0)));       	

	        	NodeList userRevenueValue = element.getElementsByTagName("userRevenueValue");
	        	temp.put("userRevenueValue", XMLUtils.getCharacterDataFromElement((Element) userRevenueValue.item(0)));
	        	
	        	NodeList snippet = element.getElementsByTagName("snippet");
	        	temp.put("snippet", XMLUtils.getCharacterDataFromElement((Element) snippet.item(0)));
	        	
	        	list.add(temp);
	        }
	        
	        //Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;		
	}
}
